//
//  EditRealCustomerVc.m
//  WGFGlobalHouses
//
//  Created by waiguofang on 15/5/28.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//  编辑真实客户

#import "EditRealCustomerVc.h"
#import "EditCustomerCell1.h"
#import "EditCustomerCell2.h"
#import "BrokerDataMange.h"
#import "PremisesModel.h"

#import "MBProgressHUD+MJ.h"
#import "BingPremiseVc.h"
#import "UserModal.h"
#import "AddRealCusModel.h"
#import "StringTool.h"
#import "EditCustomerCell3.h"
#import "UIView+circular.h"

@interface EditRealCustomerVc ()<UITableViewDataSource,UITableViewDelegate,editCustomerCell1Delegate,editCustomerCell2Delegate,BingPremiseVcDelegate>

@property (strong, nonatomic) BrokerDataMange *dataMange;
@property (strong, nonatomic) NSArray *dataArray;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) PremisesModel *premiseModel;

@property (strong, nonatomic) AddRealCusModel *addRealCusModel;
@property (strong, nonatomic) EditCustomerCell1 *cell1;

@end

@implementation EditRealCustomerVc

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.titleView setMianTitle:@"修改信息"];
    self.titleView.doneBtn.hidden = YES;
    self.tableView.allowsSelection = NO;
//    UITableViewCellSeparatorStyleNone
    [self getSiteList];
   
}

-(BrokerDataMange *)dataMange
{
    if (!_dataMange) {
        _dataMange = [[BrokerDataMange alloc]init];
    }
    return _dataMange;
}


-(void)getSiteList
{
    [MBProgressHUD showMessage:@"请稍等" toView:self.view];
    __weak typeof(self)bself = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        ResulteMod *mod = [bself.dataMange premiseslistWithUserCode:bself.customerModel.consumerCode];
        dispatch_async(dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUDForView:bself.view animated:YES];
            
            if (mod.resltState == ResultFail) {
                [MBProgressHUD showError:mod.resultTip];
            }else if (mod.resltState == ResultNetError)
            {
                [MBProgressHUD showError:@"网络错误"];
            }else
            {
                NSMutableArray *arrM = [NSMutableArray array];
                for (PremisesModel *model in mod.dataMod) {
                    if ([model.bindStatus isEqual:@"1"]) {
                        [arrM addObject:model];
                    }
                }
                bself.dataArray = nil;
                
                bself.dataArray = [[NSArray alloc]initWithArray:arrM];
                [bself.tableView reloadData];
            }
            
        });
    });
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.dataArray.count > 0) {
        return self.dataArray.count + 2;
    }
    return 2;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        self.cell1 = [EditCustomerCell1 cellWithTableView:tableView];
        self.cell1.model = self.customerModel;
        self.cell1.delegate = self;
        return self.cell1;
    }else if (indexPath.row == self.dataArray.count + 1)
    {
        EditCustomerCell2 *cell = [EditCustomerCell2 cellWithTableView:tableView];
        [cell.saveBtn viewToCircular];
        cell.delegate = self;
        return cell;
    }else
    {
        EditCustomerCell3 *cell = [EditCustomerCell3 cellWithTableView:tableView];
        cell.model = [self.dataArray objectAtIndex:indexPath.row - 1];
        
        return cell;
    }

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return [EditCustomerCell1 getHeight];
    }else if (indexPath.row == self.dataArray.count + 1)
    {
        return [EditCustomerCell2 getHeight];
    }else
    {
        return 44;
    }
}

#pragma mark editcustomercell1的代理 点击了“添加楼盘”
-(void)editCustomerCellClickLockSiteBtn
{
    BingPremiseVc *vc = [[BingPremiseVc alloc]initWithNibName:@"BingPremiseVc" bundle:nil];
    vc.coustomerCode = self.customerModel.consumerCode;
    
    vc.coustomerId = self.customerModel.consumerId;
    vc.vcState = PREVIOUSVCf_EDit;
    vc.delegate = self;
     vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark bingPremisedvc的代理，刷新tableView;
-(void)bingPremiseVcSeletedSites:(NSString *)premisesIds
{
    
    self.dataArray = [[NSArray alloc]initWithArray:[UserModal shareUserModel].editLockSiteArr];
    [self.tableView reloadData];
    
}

#pragma mark  修改后，给model 数据，并且判断用户名不为空
-(BOOL)initModel
{
    self.addRealCusModel = [[AddRealCusModel alloc]init];
    
//    self.addRealCusModel.name = [StringTool isBlankString:[UserModal shareUserModel].editName]?self.customerModel.consumerName:[UserModal shareUserModel].editName;
    if ([StringTool isBlankString:self.cell1.nameTextField.text]) {
        [MBProgressHUD showError:@"请输入用户名!"];
        return NO;
    }else
    {
        self.addRealCusModel.name = self.cell1.nameTextField.text;
        
        self.addRealCusModel.mobile = self.customerModel.consumerMobile;
        if (self.dataArray == nil || self.dataArray.count == 0) {
            self.addRealCusModel.premisesIds = @"";
        }else
        {
            NSMutableArray *seletedIdAr = [NSMutableArray array];
            
            for (int i = 0; i < self.dataArray.count; i ++)
            {
                PremisesModel *model = [self.dataArray objectAtIndex:i];
                
                
                if ([model.bindStatus isEqualToString:@"1"]) {
                    [seletedIdAr addObject:model.premisesId];
                }
                
            }
            self.addRealCusModel.premisesIds  = [seletedIdAr componentsJoinedByString:@","];
        }
        
        
        self.addRealCusModel.captch = @"";
        self.addRealCusModel.password = @"";
        self.addRealCusModel.repassword = @"";
        return YES;
    }
    
}

#pragma mark 拼接被选中的楼盘的ID
-(NSString *)getSeletedIdStr
{
    NSMutableArray *seletedIdAr = [NSMutableArray array];
    for (int i = 0; i < self.dataArray.count; i ++)
    {
        PremisesModel *model = [self.dataArray objectAtIndex:i];
        
        
        if ([model.bindStatus isEqualToString:@"1"]) {
            
            [seletedIdAr addObject:model.premisesId];
        }
        
    }
    return  [seletedIdAr componentsJoinedByString:@","];
    
}


#pragma mark EditCustomerCell2 保存信息
-(void)editCustomerCellClickSaveBtn
{
    if ([self initModel]) {
        [MBProgressHUD showMessage:@"请稍等" toView:self.view];
        
        __weak typeof(self)bself = self;
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            
            ResulteMod *mod = [bself.dataMange updateconsumerWithCustmerModel:bself.addRealCusModel withCustomerId:bself.customerModel.consumerId WithPremiseIds:[bself getSeletedIdStr]];
            dispatch_async(dispatch_get_main_queue(), ^{
                [MBProgressHUD hideHUDForView:bself.view animated:YES];
                
                if (mod.resltState == ResultFail) {
                    
                    [MBProgressHUD showError:mod.resultTip];
                }else if (mod.resltState == ResultNetError)
                {
                    [MBProgressHUD showError:@"网络错误"];
                }else
                {
                    [MBProgressHUD showSuccess:@"保存成功"];
                    [bself.navigationController popViewControllerAnimated:YES];
                }
                
            });
        });

    }
    
}
@end
